part of _;

extension on LoginPage {
  void controller_refreshCode() {
    codeParams = null;
    setState(() {});
    yRun(() => setState(() => codeParams = textController_account_loginname.text.isEmpty ? null : "?loginname=${textController_account_loginname.text}&t=${DateTime.now().microsecond}"));
  }

  void controller_initLoginType() {
    if (IFinal.Configs_Server.login.account) {
      setState(() => loginType = Enum_LoginType.Account);
      return;
    }
    if (IFinal.Configs_Server.login.code_server) {
      setState(() => loginType = Enum_LoginType.Code_server);
      return;
    }
    if (IFinal.Configs_Server.login.code_sdk && !yIsWeb) {
      setState(() => loginType = Enum_LoginType.Code_sdk);
      return;
    }
    if (IFinal.Configs_Server.login.verify && !yIsWeb) {
      setState(() => loginType = Enum_LoginType.Verify);
      return;
    }
    if (IFinal.Configs_Server.login.third && !yIsWeb) {
      setState(() => loginType = Enum_LoginType.Third);
      return;
    }
  }

  void controller_showMoreFunc() {
    yShowActionSheet("请选择选项", ["其它方式登录"], (index, action) {
      switch (action) {
        case "其它方式登录":
          if (loginType == Enum_LoginType.UnKnown) {
            yToast("未启用任何登录方式");
            return;
          }
          List<String> actions = [];
          if (IFinal.Configs_Server.login.account) actions.add(Enum_LoginType.Account.rawValue!);
          if (IFinal.Configs_Server.login.third && !yIsWeb) actions.add(Enum_LoginType.Third.rawValue!);
          if (IFinal.Configs_Server.login.verify) actions.add(Enum_LoginType.Verify.rawValue!);
          if (IFinal.Configs_Server.login.code_server) actions.add(Enum_LoginType.Code_server.rawValue!);
          if (IFinal.Configs_Server.login.code_sdk) actions.add(Enum_LoginType.Code_sdk.rawValue!);
          yShowActionSheet("请选择登录方式", actions, (index, action) {
            setState(() => loginType = Enum_LoginType.from(action));
          });
          break;
      }
    });
  }

  void controller_login({ShareSDKPlatform? thirdPlatform}) {
    void loginSuccess(UserModel userModel) {
      yToast("登录成功");
      AppUtils.saveUserModel(userModel);
      AppUtils.goHome();
    }

    if (!isSelectProtocol) {
      yToast("请阅读并同意协议");
      return;
    }
    switch (loginType) {
      case Enum_LoginType.Account:
        final loginname = textController_account_loginname.text;
        if (loginname.isEmpty) {
          yToast("请输入用户名");
          return;
        }
        final code = textController_account_code.text;
        if (code.isEmpty) {
          yToast("请输入验证码");
          return;
        }
        AppUtils.check_account(
          yPage,
          textController_account_password.text,
          loginname: loginname,
          code: code,
          onSuccess: (userModel) => loginSuccess(userModel),
          onFail: () => controller_refreshCode(),
        );
        break;
      case Enum_LoginType.Third:
        AppUtils.check_third(yPage, thirdPlatform, (userModel) => loginSuccess(userModel));
        break;
      case Enum_LoginType.Verify:
        AppUtils.check_verify(yPage, (userModel) => loginSuccess(userModel));
        break;
      case Enum_LoginType.Code_server:
        AppUtils.check_sms_server(yPage, textController_code_server_phone.text, textController_code_server_code.text, (userModel) => loginSuccess(userModel));
        break;
      case Enum_LoginType.Code_sdk:
        AppUtils.check_sms_sdk(yPage, textController_code_server_phone.text, textController_code_server_code.text, (userModel) => loginSuccess(userModel));
        break;
      case Enum_LoginType.UnKnown:
        yToast("尚未支持的登录方式");
        break;
    }
  }

  void controller_runSmsTimer() {
    setState(() => codeTimeLeft = 60);
    timer_code = yRunLoop((timer) {
      if (codeTimeLeft > 0)
        setState(() => codeTimeLeft--);
      else
        timer_code?.cancel();
    }, 1000);
  }
}
